﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Text;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class HLTGBMOVData : BaseFunction
    {
        public bool flagstartmtr;
        public bool flagRegenerateLastMonthData;
        string InvDate;
        string LASTINVDATE;
        ArrayList ListGBInventory;
        ArrayList ListTris = new ArrayList();
        string fileGBDATINV = Path.Combine(PathHelper.ApplicationPath, @"Report\Warehouse\GBDATINV.xml");
        DataSet setGBDATINV = new DataSet();
        DataTable tbGBDATINV;
        List<GBMOVEMENT> ListGBMOVEMENT;
        List<WGSummary> ListWGSummary;
        string TrisInclude = "";
        public int ThreadCount;
        private string OrderNum;
        DataTable tbGBMOVEMENT;
        public HLTGBMOVData()
            : base()
        {
            BuildTrisList();
            tbGBMOVEMENT = new DataTable("ICC_GBMOVSUM");
            tbGBMOVEMENT.Columns.Add("ICCGLDAT", typeof(int));
            tbGBMOVEMENT.Columns.Add("ARTICLE", typeof(string));
            tbGBMOVEMENT.Columns.Add("ARTICLEDES", typeof(string));
            tbGBMOVEMENT.Columns.Add("COLOR", typeof(string));
            tbGBMOVEMENT.Columns.Add("COLORDES", typeof(string));
            tbGBMOVEMENT.Columns.Add("SPPART", typeof(string));
            tbGBMOVEMENT.Columns.Add("FBPART", typeof(string));
            tbGBMOVEMENT.Columns.Add("TYPE", typeof(string));
            tbGBMOVEMENT.Columns.Add("SUPPLIER", typeof(string));
            tbGBMOVEMENT.Columns.Add("DES", typeof(string));
            tbGBMOVEMENT.Columns.Add("ORDERNO", typeof(string));
            tbGBMOVEMENT.Columns.Add("STARTWEI", typeof(double)); 
            tbGBMOVEMENT.Columns.Add("INWEI", typeof(double)); 
            tbGBMOVEMENT.Columns.Add("OUTWEI", typeof(double)); 
            tbGBMOVEMENT.Columns.Add("ADJWEI", typeof(double));
            tbGBMOVEMENT.Columns.Add("ENDWEI", typeof(double)); 
            setGBDATINV.Merge(tbGBMOVEMENT);

        }
        public HLTGBMOVData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        private void BuildTrisList()
        {
            ListTris = new ArrayList();
            TrisType tristype = null;
            TrisInclude = "01;10;11;13;";
            tristype = new TrisType("01", "+", "净长"); ListTris.Add(tristype);
            tristype = new TrisType("10", "+", "货物收入"); ListTris.Add(tristype);
            tristype = new TrisType("11", "+", "货物收入"); ListTris.Add(tristype);
            tristype = new TrisType("12", "-", "销售返回"); ListTris.Add(tristype);
            tristype = new TrisType("13", "+", "产品录入"); ListTris.Add(tristype);
            tristype = new TrisType("20", "-", "货物发出"); ListTris.Add(tristype);
            tristype = new TrisType("21", "+", "货物进入"); ListTris.Add(tristype);
            tristype = new TrisType("26", "-", "旧布卷减少"); ListTris.Add(tristype);
            tristype = new TrisType("27", "+", "新布卷增加"); ListTris.Add(tristype);
            tristype = new TrisType("31", "-", "发货"); ListTris.Add(tristype);
            tristype = new TrisType("32", "+", "退货"); ListTris.Add(tristype);
            tristype = new TrisType("36", "-", "生产消耗"); ListTris.Add(tristype);
            tristype = new TrisType("40", "-", "等级修改减少"); ListTris.Add(tristype);
            tristype = new TrisType("41", "-", "等级修改增加"); ListTris.Add(tristype);
            tristype = new TrisType("42", "+", "库存修正减少"); ListTris.Add(tristype);
            tristype = new TrisType("43", "+", "库存修正增加"); ListTris.Add(tristype);
            tristype = new TrisType("48", "-", "生产差异-"); ListTris.Add(tristype);
            tristype = new TrisType("49", "+", "生产差异+"); ListTris.Add(tristype);
            tristype = new TrisType("51", "+", "消耗差异"); ListTris.Add(tristype);
            tristype = new TrisType("61", "-", "留位"); ListTris.Add(tristype);
            tristype = new TrisType("62", "+", "取消留位"); ListTris.Add(tristype);
            tristype = new TrisType("64", "+", "留位剪布"); ListTris.Add(tristype);
            tristype = new TrisType("65", "+", "锁定"); ListTris.Add(tristype);
            tristype = new TrisType("66", "+", "解锁"); ListTris.Add(tristype);
            tristype = new TrisType("69", "+", "其他修正"); ListTris.Add(tristype);
            tristype = new TrisType("80", "+", "发票数量"); ListTris.Add(tristype);
            tristype = new TrisType("88", "-", "价值更改-"); ListTris.Add(tristype);
            tristype = new TrisType("89", "+", "价值更改"); ListTris.Add(tristype);
            tristype = new TrisType("IN", "+", "系统自动调节"); ListTris.Add(tristype);

        }
   
        public DataSet LoadICC_GLLG(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string VKPAUFNUM;
            string WBSRTDATE, WBENTDATE, INVDATE;
            WBSRTDATE = dwABPOS["WBSRTDATE"].ToString().Trim();
            WBENTDATE = dwABPOS["WBENTDATE"].ToString().Trim();
            INVDATE = GetDateAddDays(WBSRTDATE,-1);

            try
            {
                #region ==============GET the INV Data
                OracleCommand or1 = OracleDB.CreateCommand();
                #region

                or1.CommandText = "SELECT A.ICCGLDAT,DECODE(TRIM(B.GPGVAR),'',TRIM(B.GPGART),TRIM(B.GPGART)||'-'||B.GPGVAR) AS ARTICLE,C.GSGARNBEZ,"
                    + " B.GPGFARBE,D.GSCBEZ,B.GPSPPART,B.GPFBPART,'AAA' AS TYPE,'' AS SUPPLIER, "
                    + " '' AS DES,'' AS ORDERNO, "
                    + " ROUND(SUM(A.ICCGLBSTNET/1000),1) AS STARTWEI,0 AS INWEI,0 AS OUTWEI, "
                    + " 0 AS ADJWEI,ROUND(SUM(A.ICCGLBSTNET/1000),1) AS ENDWEI "
                    + "  FROM ICC_GLLG A,GPDAT B,GSDAT C,GSCOLOR D,LIEFDAT E "
                    + " WHERE A.ICCGLMANDANT = B.GPMANDANT "
                    + " AND A.ICCGLPARTIE = B.GPPARTIE AND A.ICCGLPARTIEKZ = B.GPPARTIEKZ "
                    + " AND B.GPMANDANT = C.GSMANDANT(+) AND B.GPGART = C.GSART(+) "
                    + " AND B.GPGVAR = C.GSVAR(+) AND B.GPMANDANT = D.GSCMANDANT(+) "
                    + " AND B.GPGART = D.GSCART(+) AND B.GPGVAR = D.GSCVAR(+) AND A.ICCGLLB IN ('10','11','12','13') "
                    + " AND B.GPGFARBE = D.GSCFARBE(+) AND A.ICCGLDAT = '" + INVDATE + "'"
                    + " GROUP BY A.ICCGLDAT,DECODE(TRIM(B.GPGVAR),'',TRIM(B.GPGART),TRIM(B.GPGART)||'-'||B.GPGVAR),"
                    + " C.GSGARNBEZ,B.GPGFARBE,D.GSCBEZ,B.GPSPPART,B.GPFBPART  "
                   ;
                #endregion
                setLocal = Connect2Proxy(or1);
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ICC_GLLG";
                    SetDefault(tableLocal);
                    byte[] b;
                    string value;
                    string Column = "";
                    DataRow dwGBMOVEMENT = null;
                    List<DataRow> dltRows = new List<DataRow>();
                    ListGBMOVEMENT = new List<GBMOVEMENT>();
                    tbGBMOVEMENT = setGBDATINV.Tables["ICC_GBMOVSUM"];
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        Column = "GSGARNBEZ";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "GSCBEZ";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "SUPPLIER";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "DES";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        dwGBMOVEMENT = tbGBMOVEMENT.NewRow();
                        dwGBMOVEMENT.ItemArray = dwTemp.ItemArray;
                        tbGBMOVEMENT.Rows.Add(dwGBMOVEMENT);
                    }
                }
                #endregion
                #region Build ListGBInventory 
                string ARTICLE, COLOR, SPPART, FBPART;
                string LASTDAT;
                GBInventory wbInv;
                double LASTWEI;
                ListGBInventory = new ArrayList();
                foreach (DataRow dwTemp in tbGBMOVEMENT.Rows)
                {
                    ARTICLE = dwTemp["ARTICLE"].ToString();
                    COLOR = dwTemp["GPGFARBE"].ToString();
                    SPPART = dwTemp["GPSPPART"].ToString();
                    FBPART = dwTemp["GPFBPART"].ToString();

                    LASTDAT = dwTemp["ICCGLDAT"].ToString();
                    LASTWEI = System.Math.Round(System.Convert.ToDouble(dwTemp["STARTMTR"].ToString()), 1);
                    wbInv = new GBInventory(LASTDAT, ARTICLE, COLOR, SPPART, FBPART, LASTWEI);
                    ListGBInventory.Add(wbInv);
                } 
                #endregion
                set4Return.Merge(setLocal);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        public DataSet GetRawData(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string VKPAUFNUM;
            string WBSRTDATE, WBENTDATE;
            WBSRTDATE = dwABPOS["WBSRTDATE"].ToString().Trim();
            WBENTDATE = dwABPOS["WBENTDATE"].ToString().Trim();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                #region
                /*
                
                */
                or1.CommandText = "select A.ICCWBBDAT AS ICCWBMDAT,'AAA' AS ICCWBMTYPE,A.ICCWBBWAKZ AS ICCWBMWAKZ, "
                    + " '' AS ICCWBMBS,0 AS ICCWBMBELEG,TRIM(A.ICCWBBART)||'-'||TRIM(A.ICCWBBFARBE)||'-'||A.ICCWBBVAR AS ARTICLE, "
                    + " A.ICCWBBBESCHICH AS ICCWBMBESCHICH,A.ICCWBBDPART AS ICCWBMDPART,'' AS ICCWBMCODE, "
                    + " '' AS ICCWBMNAME,'' AS ICCWBMBDES,'' AS ICCWBMAUFTYP,'' AS ORDERNO,ROUND(SUM(A.ICCWBBSSTKLG/1000/0.9144),1) AS STARTMTR, "
                    + " COUNT(A.ICCWBBSTKNR) AS STARTPCNUM,0 AS INMTR,0 AS INPCNUM,0 AS OUTMTR,0 AS OUTPCNUM,0 AS ADJMTR, "
                    + " ROUND(SUM(A.ICCWBBSSTKLG/1000/0.9144),1) AS ENDMTR,COUNT(A.ICCWBBSTKNR) AS ENDPCNUM "
                    + " from icc_wbbst A where A.ICCWBBDAT = '" + WBSRTDATE + "'"
                    + " AND A.ICCWBBWAKZ = 'F' AND NOT EXISTS( "
                    + " SELECT '1' FROM ICC_WBMOV B WHERE B.ICCWBMWAKZ = 'F' AND A.ICCWBBWAKZ = B.ICCWBMWAKZ AND A.ICCWBBART = B.ICCWBMART "
                    + " AND A.ICCWBBVAR = B.ICCWBMVAR AND A.ICCWBBFARBE = B.ICCWBMFARBE "
                    + " AND A.ICCWBBBESCHICH = B.ICCWBMBESCHICH  AND A.ICCWBBDPART = B.ICCWBMDPART "
                    + " AND B.ICCWBMDAT >='" + WBSRTDATE + "' AND B.ICCWBMDAT <='" + WBENTDATE + "') "
                    + " GROUP BY A.ICCWBBDAT,A.ICCWBBWAKZ,TRIM(A.ICCWBBART)||'-'||TRIM(A.ICCWBBFARBE)||'-'||A.ICCWBBVAR, "
                    + " A.ICCWBBBESCHICH,A.ICCWBBDPART "
                    + " UNION ALL "
                    + " SELECT A.ICCWBMDAT,A.ICCWBMTYPE,A.ICCWBMWAKZ,A.ICCWBMBS,MAX(A.ICCWBMBELEG) AS ICCWBMBELEG, "
                    + "  TRIM(A.ICCWBMART)||'-'||TRIM(A.ICCWBMFARBE)||'-'||A.ICCWBMVAR AS ARTICLE, "
                    + "  A.ICCWBMBESCHICH,A.ICCWBMDPART,A.ICCWBMCODE,A.ICCWBMNAME,A.ICCWBMBDES,A.ICCWBMAUFTYP, "
                    + "  A.ICCWBMAUFKRS||'-'||TO_NUMBER(A.ICCWBMAUFLFD)||'-'||TO_NUMBER(A.ICCWBMAUFPOS) AS ORDERNO, "
                    + "  0 AS STARTMTR, 0 AS STARTPCNUM, "
                    + "  nvl(DECODE(A.ICCWBMTYPE,'IN ',ROUND(SUM(A.ICCWBMMTR/0.9144/1000),1),0),0) AS INMTR, "
                    + "  nvl(DECODE(A.ICCWBMTYPE,'IN ',SUM(CASE WHEN ICCWBMMTR <0 THEN -1 ELSE 1 END),0),0) AS INPCNUM, "
                    + "  nvl(DECODE(A.ICCWBMTYPE,'OUT',ROUND(SUM(A.ICCWBMMTR/0.9144/1000),1),0),0) AS OUTMTR, "
                    + "  nvl(DECODE(A.ICCWBMTYPE,'OUT',SUM(CASE WHEN ICCWBMMTR <0 THEN -1 ELSE 1 END),0),0) AS OUTPCNUM, "
                    + "  nvl(DECODE(A.ICCWBMTYPE,'ADJ',ROUND(SUM(A.ICCWBMMTR/0.9144/1000),1),0),0) AS ADJMTR, "
                    + "  0 AS ENDMTR,0 AS ENDPCNUM "
                    + " FROM ICC_WBMOV A "
                    + " WHERE A.ICCWBMWAKZ = 'F' AND A.ICCWBMDAT >='" + WBSRTDATE + "' AND A.ICCWBMDAT <='" + WBENTDATE + "'"
                    + " GROUP BY A.ICCWBMDAT,A.ICCWBMTYPE,A.ICCWBMWAKZ,A.ICCWBMBS, "
                    + "  TRIM(A.ICCWBMART)||'-'||TRIM(A.ICCWBMFARBE)||'-'||A.ICCWBMVAR, "
                    + "  A.ICCWBMBESCHICH,A.ICCWBMDPART,A.ICCWBMCODE,A.ICCWBMNAME,A.ICCWBMBDES,A.ICCWBMAUFTYP, "
                    + "  A.ICCWBMAUFKRS||'-'||TO_NUMBER(A.ICCWBMAUFLFD)||'-'||TO_NUMBER(A.ICCWBMAUFPOS) "
                    + "  ORDER BY 6,7,8,1,2 "
                     ;
                #endregion
                setLocal = Connect2Proxy(or1);
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ICC_WBMOV";
                    SetDefault(tableLocal);
                    byte[] b;
                    string value;
                    string Column = "";
                    List<DataRow> dltRows = new List<DataRow>(); 
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        Column = "ARTICLE";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "ICCWBMBESCHICH";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "ICCWBMDPART";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "ICCWBMNAME";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        BalanceQty(dwTemp); 
                    } 
                    set4Return.Merge(setLocal); 
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }

        private void BalanceQty(DataRow dwICC_WBMOV)
        {
            
            try
            {
                string ICCWBMDAT = dwICC_WBMOV["ICCWBMDAT"].ToString();
                string ICCWBMTYPE = dwICC_WBMOV["ICCWBMTYPE"].ToString().Trim();
                string ICCWBMWAKZ = dwICC_WBMOV["ICCWBMWAKZ"].ToString().Trim();
                string ICCWBMBS = dwICC_WBMOV["ICCWBMBS"].ToString().Trim();
                string ARTICLE = dwICC_WBMOV["ARTICLE"].ToString().Trim();
                string ICCWBMBESCHICH = dwICC_WBMOV["ICCWBMBESCHICH"].ToString().Trim();
                string ICCWBMDPART = dwICC_WBMOV["ICCWBMDPART"].ToString().Trim();
                string ICCWBMCODE = dwICC_WBMOV["ICCWBMCODE"].ToString().Trim();
                string ICCWBMNAME = dwICC_WBMOV["ICCWBMNAME"].ToString().Trim();
                string ICCWBMBDES = dwICC_WBMOV["ICCWBMBDES"].ToString().Trim();
                string ICCWBMAUFTYP = dwICC_WBMOV["ICCWBMAUFTYP"].ToString().Trim();
                string ORDERNO = dwICC_WBMOV["ORDERNO"].ToString().Trim();
                double STARTMTR = System.Math.Round(System.Convert.ToDouble(dwICC_WBMOV["STARTMTR"].ToString()),1);
                int STARTPCNUM = System.Convert.ToInt16(dwICC_WBMOV["STARTPCNUM"].ToString());
                double INMTR = System.Math.Round(System.Convert.ToDouble(dwICC_WBMOV["INMTR"].ToString()),1);
                int INPCNUM = System.Convert.ToInt16(dwICC_WBMOV["INPCNUM"].ToString());
                double OUTMTR = System.Math.Round(System.Convert.ToDouble(dwICC_WBMOV["OUTMTR"].ToString()),1);
                int OUTPCNUM = System.Convert.ToInt16(dwICC_WBMOV["OUTPCNUM"].ToString());
                double ADJMTR = System.Math.Round(System.Convert.ToDouble(dwICC_WBMOV["ADJMTR"].ToString()), 1);
                double ENDMTR = 0;
                int ENDPCNUM = 0;
                DataTable ICC_WBMOV;
                WBInventory wbm = null;
                string expression;
                var query = from WBInventory wbinv in ListGBInventory
                            where wbinv.ARTICLE == ARTICLE
                            && wbinv.FINISH == ICCWBMBESCHICH
                            && wbinv.WBBDPART == ICCWBMDPART
                            select wbinv;
                foreach (WBInventory wbinv in query)
                {
                    wbm = wbinv;
                }
                if (wbm == null)
                {
                    wbm = new WBInventory(ICCWBMDAT, "HL", ICCWBMWAKZ, ARTICLE, ICCWBMBESCHICH, ICCWBMDPART, 0, 0.0);
                    ListGBInventory.Add(wbm);
                }
                if (wbm.STARTPCNUM == 0)
                {
                    dwICC_WBMOV["STARTMTR"] = System.Math.Round((wbm.STARTQTY + wbm.ENDQTY), 1);
                    dwICC_WBMOV["STARTPCNUM"] = wbm.STARTPCNUM + wbm.ENDPCNUM;
                    wbm.ENDPCNUM = wbm.STARTPCNUM + wbm.ENDPCNUM + INPCNUM - OUTPCNUM;
                    dwICC_WBMOV["ENDPCNUM"] = wbm.ENDPCNUM;
                    wbm.ENDQTY = System.Math.Round((wbm.STARTQTY + wbm.ENDQTY + INMTR - OUTMTR + ADJMTR), 1);
                    dwICC_WBMOV["ENDMTR"] = wbm.ENDQTY;
                    if (wbm.ENDQTY == 0)
                    {
                        dwICC_WBMOV["ENDPCNUM"] = 0;
                    }
                }
                else
                {
                    dwICC_WBMOV["STARTMTR"] = System.Math.Round(wbm.ENDQTY, 1);
                    dwICC_WBMOV["STARTPCNUM"] = wbm.ENDPCNUM;
                    wbm.ENDPCNUM = wbm.ENDPCNUM + INPCNUM - OUTPCNUM;
                    dwICC_WBMOV["ENDPCNUM"] = wbm.ENDPCNUM;
                    wbm.ENDQTY = System.Math.Round((wbm.ENDQTY + INMTR - OUTMTR + ADJMTR), 1);
                    dwICC_WBMOV["ENDMTR"] = wbm.ENDQTY;
                    if (wbm.ENDQTY == 0)
                    {
                        dwICC_WBMOV["ENDPCNUM"] = 0;
                    }
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public DataSet GetCOLORDES(DataSet dsTarget, DataRow dwABPOS)
        {
            string TableName = "COLORDES";
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT DISTINCT SUBSTR(SDDATEN,2,30) AS COLORDES FROM SDDAT "
                    + " WHERE SUBSTR(SDKEY,1,2)='00' AND trim(SUBSTR(SDKEY,3,10))=:ABPBESCHICH  AND SDSA = '9998' "
                     ;
                orlReadCommand.Parameters.Add("ABPBESCHICH", OracleType.NVarChar).Value = dwABPOS["WBBBESCHICH"].ToString().Trim();
                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = TableName;

                    byte[] b;
                    string value;
                    string ColumnName = "";
                    foreach (DataRow dw in dsResult.Tables["COLORDES"].Rows)
                    {
                        ColumnName = "COLORDES";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[ColumnName].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw[ColumnName] = value;

                        dwABPOS["COLORDES"] = dw["COLORDES"];
                    }
                }
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    if (dsTarget.Tables.Contains(TableName))
                    {
                        dsTarget.Tables[TableName].Clear();
                    }
                    dsTarget = ImportDataTable(dsTarget, dtTemp);
                }
                dsTarget.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dsTarget;
        }
        public DataSet BuildWBBST(DataRow dwUI)
        {
            string progress = "查询完成";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);

                EventDispatcher.Instance.RaiseStatus("装载结存表", StatusEventType.RunningProgress);
                LoadICC_GLLG(dwUI);
                tbGBDATINV = setGBDATINV.Tables["ICC_GBMOVSUM"];

                EventDispatcher.Instance.RaiseStatus("装载出入库记录", StatusEventType.RunningProgress);
                setResult = GetRawData(dwUI);
                
                EventDispatcher.Instance.RaiseStatus("计算结存", StatusEventType.RunningProgress);
                setReturn.Merge(setResult);
                EventDispatcher.Instance.RaiseStatus("保存结存表", StatusEventType.RunningProgress);
                //SaveINVQty();
                //setReturn.Merge(setWBDATINV);
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
             
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}